<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/31
 * Time: 上午10:08
 */

namespace App\Modules\User\Models;


use Illuminate\Database\Eloquent\Model;

class UserBill extends Model
{
    public static $_TYPE_Pay = "Pay";//支付
    public static $_TYPE_Withdraw = "Withdraw";//提现
    public static $_TYPE_Refund = "Refund";//退款
    public static $_TYPE_RewardIn = "RewardIn";//打赏进钱
    public static $_TYPE_RewardOut = "RewardOut";//打赏出钱

    protected $table = 'user_bills';
    protected $fillable = ['uid', 'amount', 'type', 'title', 'remark', 'task_id' , 'order_id', 'pay_withdraw_cash_id'];


    /**
     * 用户流水列表
     * @param $params[]
     *                  uid:用户ID 可选
     *                  type:流水类型
     *                  start_time:开始时间 可选
     *                  end_time:结束时间 可选
     * @param $isAll
     * @return mixed
     */
    public static function getList($params=[], $isAll = false) {
        $sqlObj = self::orderBy('created_at','desc');

        if(isset($params['uid']) && $params['uid']) {
            $sqlObj->where('uid','=', $params['uid']);
        }

        if(isset($params['type']) && $params['type']) {
            $sqlObj->where('type', $params['type']);
        }

        if(isset($params['start_time']) && $params['start_time']) {
            $sqlObj->where('created_at','>=', $params['start_time']);
        }
        if(isset($params['end_time']) && $params['end_time']) {
            $sqlObj->where('created_at','<=', $params['end_time']);
        }

        if($isAll) {
            return $sqlObj->get();
        } else {
            return $sqlObj->paginate();
        }
    }
}